package com.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.reggie.exception.CustomException;
import com.reggie.mapper.CategoryMapper;
import com.reggie.pojo.Category;
import com.reggie.pojo.Dish;
import com.reggie.pojo.Setmeal;
import com.reggie.service.CategoryService;
import com.reggie.service.DishService;
import com.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private SetmealService setmealService;

    @Autowired
    private DishService dishService;

    /**
     * 根据id删除分类，删除之前做一个查询，是否关联菜品或套餐
     * @param id
     */
    @Override
    public String remove(Long id) {
       //查看是否可以删除
        String result = checkCategoryWithDishOrSetmeal(id);

        if (result != null) {
            //有关联信息,不可删除
            return result;
        }
        //正常删除
        super.removeById(id);
        return null;
    }

    /**
     * 将分类下的菜品移动至id排序第一的分类中
     * @param id
     */
    @Override
    @Transactional
    public void move(Long id) {
        //首先查询分类下是否有菜品
        checkCategoryWithDishOrSetmeal(id);

        //获取可移动的菜品分类Id
        Long categoryId = findCategory(id);

        //根据分类ID查询菜品列表
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dish::getCategoryId, id);
        List<Dish> dishList = dishService.list(wrapper);

        //遍历菜品更改分类ID
        for (Dish dish : dishList) {
            dish.setCategoryId(categoryId);
            //更新数据库中菜品信息
            dishService.updateById(dish);
        }

    }

    /**
     * 查询分类下是否有菜品
     * @param id
     */
    private String checkCategoryWithDishOrSetmeal(Long id) {
        //添加查询条件
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(Dish::getCategoryId, id);

        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId, id);

        //查询当前分类是否关联了菜品，如果已经关联，抛出业务异常
        if (dishService.count(dishLambdaQueryWrapper) > 0) {
            return "关联了菜品";
            //已将关联，抛出异常
//            throw new CustomException("当前分类下关联了菜品，不能删除");
        }

        //查询当前分类是否关联了套餐，如果已经关联，抛出业务异常
        if (setmealService.count(setmealLambdaQueryWrapper) > 0) {
            return "关联了套餐";
            //已经关联，抛出异常
//            throw new CustomException("当前分类下关联了套餐，不能删除");
        }
        return null;
    }

    /**
     * 查询可用的分类Id,除去当前分类id以外
     * @param id 当前分类Id
     * @return
     */
    private Long findCategory(Long id) {
        Long findId = 0L;
        while (true) {
            //条件构造器
            LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Category::getId,findId);
            //查询分类
            int count = this.count(wrapper);
            if (count > 0 && !findId.equals(id)) {
                //该分类存在,返回菜品Id并且结束死循环,并且该分类不是当前删除的分类
                return findId;
            }
            //未查到该菜品
            findId += 1;
        }
    }
}
